from peewee import *
from search_order_srv.config import config
from time import time


class BaseModel(Model):
    create_time = BigIntegerField(default=time() * 1000, null=False, verbose_name="创建时间")
    update_time = BigIntegerField(default=0, null=False, verbose_name="更新时间")
    del_time = IntegerField(default=0, null=False, verbose_name="删除时间")

    def save(self, *args, **kwargs):
        self.update_time = time() * 1000
        return super(BaseModel, self).save(*args, **kwargs)

    @classmethod
    def select(cls, *selection):
        # 在大多数情况下,此方法有效,但在某些情况下,如果使用带有关键字“ IN”的联接结束结果查询时出现错误,则选择它会中断：“ 1241,’操作数应包含1列”
        return super(BaseModel, cls).select(cls, *selection).where(cls.del_time == 0)

    def soft_delete(self):
        self.del_time = time()
        return super(BaseModel, self).save()

    class Meta:
        database = config.DB


STATUS_CHOICES = (
    (0, "禁用"),
    (1, "启用")
)


class Order(BaseModel):
    """
       订单
       """
    ORDER_STATUS = (
        (-1, "订单关闭"),
        (0, "待审核"),
        (1, "待认领"),
        (2, "完成"),
        (3, "审核失败"),
    )

    uid = IntegerField(null=False, verbose_name="用户id")
    type = IntegerField(null=False, default=0, verbose_name="订单类型, 1找物品，2找失主")
    order_sn = CharField(max_length=30, null=True, unique=True, verbose_name="订单号")
    status = IntegerField(null=False, choices=ORDER_STATUS, default=0, verbose_name="订单状态")
    category_id = IntegerField(null=False, verbose_name="分类id")

    # 物品信息
    title = CharField(null=False, verbose_name="标题")
    order_img = IntegerField(null=False, verbose_name="物品图片")
    desc = TextField(null=False, default="", verbose_name="物品的描述")

    # 认领人信息
    claim_address = CharField(max_length=100, default="", verbose_name="认领人地址")
    claim_name = CharField(max_length=20, default="", verbose_name="认领人/拾物人 ")
    claim_mobile = CharField(max_length=11, verbose_name="联系电话")
    about = CharField(max_length=20, default="", verbose_name="留言")

    check_time = BigIntegerField(null=True, default=0, verbose_name="审核时间")
    claim_time = BigIntegerField(null=True, default=0, verbose_name="认领时间")


class Category(BaseModel):
    pid = IntegerField(default=0, null=False, verbose_name="父级id")
    level = IntegerField(default=1, null=False, verbose_name="层级")
    title = CharField(null=False, verbose_name="分类")


class OrderImg(BaseModel):
    order_id = IntegerField(null=False, verbose_name="订单id")
    img_url = CharField(null=False, verbose_name="图片地址")


class OrderMathImg(BaseModel):
    order_id = IntegerField(null=False, verbose_name="订单id")
    math_order_id = IntegerField(null=False, verbose_name="订单id")
    order_title = CharField(null=False, verbose_name="订单标题")
    math_order_title = CharField(null=False, verbose_name="匹配订单标题")
    img_url = CharField(null=False, verbose_name="图片地址")
    math_img_url = CharField(null=False, verbose_name="匹配图片地址")
    optimal = IntegerField(null=False, default=0, verbose_name="图片相似度%")
    math_time = BigIntegerField(null=False, verbose_name="匹配时间")


if __name__ == '__main__':
    models = [Order, OrderImg, OrderMathImg, Category]
    config.DB.create_tables(models)
    print("创建成功")
